package com.shixi.controller;

import com.shixi.domain.User;
import com.shixi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/toupdatePhone")
    public String toupdatePhone(Model model, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        String phone = loginUser.getPhone();
        model.addAttribute("phone", phone);
        return "update/updatePhone";
    }

    @RequestMapping("/updatePhoneSubmit")
    @ResponseBody
    public Map<String,Object> updatePwd(String newPhone, HttpServletRequest request)
    {
        System.out.println("进入修改手机号的控制器！");

        User user = (User) request.getSession().getAttribute("loginUser");
        String username = user.getUsername();

        /*调用UserService内的修改手机号的业务*/
        boolean updateresult = userService.updatePhone(username, newPhone, request);
        HashMap<String,Object> map = new HashMap<>();
        if(updateresult==true)
        {
            map.put("code",0);
        }
        else {
            map.put("code",-1);
        }
        return map;
    }


}
